package durose.modular.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import durose.system.dict.CommonDict;
import durose.system.pojo.Dept;
 import durose.system.service.DeptService; 


import durose.system.pojo.Boss; 
import durose.system.service.BossService;
 import durose.system.springUtil.SpringBossFactory; 

 import com.github.pagehelper.Page;
import durose.system.dict.ResponseDict;

@Controller
public class BossController {
	String prefix = "system/modular/boss/";
	
	@Autowired 
 	 DeptService deptService;

	
	@Autowired
	BossService bossService;
	
	
	@RequestMapping("boss")
	public String bossGet(){
		return prefix+"boss";
	}
	
	@RequestMapping("boss_list")
	@ResponseBody
	public Object bossList(@RequestParam(value="pageNumber",defaultValue="0")Integer pagenumber,@RequestParam(value="queryName",required=false) String queryName){
		PageHelper.startPage(pagenumber,CommonDict.PAGE_SIZE);
		List<Boss> bosses = bossService.bossList(queryName);
		//把从数据库中查询的pojo数据与所关联的表进行处理(如果有关联的话)
		List<Map<String,Object>> mapList = SpringBossFactory.me().initData((Page)bosses);
		PageInfo<Map<String,Object>> page = new PageInfo<>(mapList);
		return page;
	}
	
	@RequestMapping("boss_add")
	@ResponseBody
	public Object bossAdd(Boss pojo){
		bossService.bossAdd(pojo);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	@RequestMapping("boss_add_page")
	public String bossAddPage(Model model){
		model.addAttribute("depts", deptService.deptList(""));

		return prefix+"boss_add";
	}
	
	@RequestMapping("boss_edit")
	@ResponseBody
	public Object bossEdit(Boss pojo){
		bossService.bossUpdate(pojo);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	@RequestMapping("boss_edit_page")
	public String  bossEditPage(int id,Model model){
		model.addAttribute("depts", deptService.deptList(""));

		model.addAttribute("boss", bossService.bossGet(id));
		return prefix+"boss_edit";
	}
	
	@RequestMapping("boss_delete")
	@ResponseBody
	public Object bossDelete(int id){
		bossService.bossDelete(id);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	
}
